package com.exposit.zbs.service.impl;

import com.exposit.zbs.dao.IMessagesDAO;
import com.exposit.zbs.dao.IUserDAO;
import com.exposit.zbs.entity.MessageEntity;
import com.exposit.zbs.entity.UserEntity;
import com.exposit.zbs.model.Message;
import com.exposit.zbs.model.User;
import com.exposit.zbs.service.IUserService;
import com.exposit.zbs.utils.MapperProvider;
import com.exposit.zbs.utils.MessageDigestHelper;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class UserService implements IUserService {

	@Autowired
	private IUserDAO userDAO;

	@Autowired
	private IMessagesDAO messagesDAO;
	public String getString() {

		return "test";
	}

	@Override
	public void saveUser(User user) {

		Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
		UserEntity userEntity = new UserEntity();
		mapper.map(user, userEntity);
		if (userEntity != null) {

			userEntity.setUserPassHash(MessageDigestHelper.getMd5(userEntity.getUserPassHash()));
			userDAO.save(userEntity);
		}

	}

	@Override
	public void updateUserData(User user) {

		UserEntity userEntity = new UserEntity();
		MapperProvider.getDozerMapper().map(user, userEntity);
		userDAO.update(userEntity);
	}

	@Override
	public User getUserbyId(Long id) {

		UserEntity userEntity = userDAO.findById(id);
		if (userEntity != null) {
			Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
			User user = new User();
			mapper.map(userEntity, user);
			return user;
		} else {
			return null;
		}
	}

	@Override
	public User getUserbyLogin(String login) {

		UserEntity findResult = userDAO.findByLogin(login);
		User user = new User();
		if (findResult != null) {
			Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
			mapper.map(findResult, user);
			return user;
		} else {
			return null;
		}
	}

	@Override
	public boolean checkUserExist(User user) {

		return userDAO.checkExistingByLoginOrEmail(user.getUserLogin(), user.getUserEmail());
	}

	@Transactional
	public List<User> getAllUsers() {

		List<UserEntity> userEntityList = userDAO.findAll();
		Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
		List<User> userList = new ArrayList<User>();
		User t = null;
		for (UserEntity userEntity : userEntityList) {
			t = new User();
			mapper.map(userEntity, t);
			userList.add(t);
		}
		return userList;
	}
	@Override
	public void sendMessage(Message message){

		MessageEntity me = new MessageEntity();
		MapperProvider.getDozerMapper().map(message, me);
		messagesDAO.save(me);

	}

	@Override
	public void readMessages(User user) {
		messagesDAO.readMessages(user.getUserId());
	}
}
